package com.ct.R.Rerror;

import com.ct.R.IResultCodeInterface;
import lombok.Data;

@Data
public class Rerror {
    // RError === Result 答案：不想破坏Result类。
    // 异常的状态码，从枚举中获得
    private Integer code;
    // 状态名字
    private String codeDesc;
    // 异常的消息，写用户看得懂的异常，从枚举中得到
    private Object msg;
    // 属性名字
    private String field;
    // 异常的名字
    private String exception;


    public static Rerror fail(Integer code, String codeDesc,Object msg, String field,  Throwable throwable) {
        Rerror rError = new Rerror();
        rError.setMsg(msg);
        rError.setCode(code);
        rError.setCodeDesc(codeDesc);
        rError.setField(field);
        rError.setException(throwable.getMessage());
        return rError;
    }


    public static Rerror fail(Integer code, String codeDesc, Object msg, Throwable throwable) {
        Rerror rError = new Rerror();
        rError.setMsg(msg);
        rError.setCode(code);
        rError.setCodeDesc(codeDesc);
        rError.setException(throwable.getMessage());
        return rError;
    }

    public static Rerror fail(IResultCodeInterface iResultCodeInterface, Throwable throwable) {
        Rerror rError = new Rerror();
        rError.setMsg(iResultCodeInterface.getMsg());
        rError.setCodeDesc(iResultCodeInterface.getCodeDesc());
        rError.setCode(iResultCodeInterface.getCode());
        rError.setException(throwable.getMessage());
        return rError;
    }
}
